﻿using System;
using System.Web.UI.DataVisualization.Charting;
using Resources;
using Yglu.BusinessLayer;
using Yglu.BusinessLayer.Entities;
using Yglu.BusinessLayer.Enums;

namespace Yglu.WebAdmin
{
	public partial class Reports_aspx : BasePage
	{
		#region Main methods

		protected void Page_Load(object sender, EventArgs e)
		{
			CheckPermissions(AdminLevel.Store);

			if (!IsPostBack)
			{
				SetDefaults();
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			Period period = new Period();

			period.DateFrom = Convert.ToDateTime(TxtDateFrom.Text + " 00:00:00");
			period.DateTo = Convert.ToDateTime(TxtDateTo.Text + " 23:59:59");

			String report = CboReports.SelectedItem.Text;

            if (report == GetLocalResourceObject("ReportOrdersGeneral").ToString())
			{
				LoadOrders_General(period);
			}
			else if (report == GetLocalResourceObject("ReportOrdersByWeekDay").ToString())
			{
				LoadOrders_ByWeekDay(period);
			}
			else if (report == GetLocalResourceObject("ReportOrdersByUser").ToString())
			{
				LoadOrders_ByUser(period);
			}
			else if (report == GetLocalResourceObject("ReportOrdersByStore").ToString())
			{
				LoadOrders_ByStore(period);
			}

			ChartOrders.Titles.Add(report + ": " + period.DateFrom.ToString("d") + " - " + period.DateTo.ToString("d"));

			SetDefaults(ChartOrders);
		}

		private void SetDefaults()
		{
			CboReports.Items.Add(GetLocalResourceObject("ReportOrdersGeneral").ToString());
			CboReports.Items.Add(GetLocalResourceObject("ReportOrdersByWeekDay").ToString());
			CboReports.Items.Add(GetLocalResourceObject("ReportOrdersByUser").ToString());

			if (SessionManager.LoggedUser.AdminLevel >= AdminLevel.Company)
			{
				CboReports.Items.Add(GetLocalResourceObject("ReportOrdersByStore").ToString());
			}

			CboReports.SelectedIndex = 0;

			TxtDateFrom.Text = DateTime.Now.AddMonths(-1).ToString("d");
			TxtDateTo.Text = DateTime.Now.ToString("d");
		}

		#endregion

		#region Load reports

		private OrderCollection LoadOrders(Period period)
		{
			OrderCollection orders = new OrderCollection(DataAccess.Order_Get(SessionManager.LoggedStore, period));

			foreach (Order o in orders)
			{
				o.Combos = new ComboCollection(DataAccess.Combo_Get(o));
			}

			return orders;
		}

		private void LoadOrders_General(Period period)
		{
			OrderCollection orders = LoadOrders(period);

			Series sOrderCount = new Series(Messages.Orders);
			Series sComboCount = new Series(Messages.Combos);

			sOrderCount.ChartType = SeriesChartType.Line;
			sComboCount.ChartType = SeriesChartType.Line;

			for (DateTime dt = period.DateFrom; dt <= period.DateTo; dt = dt.AddDays(1))
			{
				Int32 orderCount = 0;
				Int32 comboCount = 0;

				foreach (Order o in orders)
				{
					if (o.DateOrdered.ToString("yyMMdd") == dt.ToString("yyMMdd"))
					{
						orderCount += 1;
						comboCount += o.Combos.Count;
					}
				}

				sOrderCount.Points.AddXY(dt.ToString("dd-MM"), orderCount);
				sComboCount.Points.AddXY(dt.ToString("dd-MM"), comboCount);
			}

			ChartOrders.Series.Add(sOrderCount);
			ChartOrders.Series.Add(sComboCount);
		}

		private void LoadOrders_ByWeekDay(Period period)
		{
			OrderCollection orders = LoadOrders(period);

			Series sOrderCount = new Series(Messages.Orders);
			Series sComboCount = new Series(Messages.Combos);

			sOrderCount.ChartType = SeriesChartType.Column;
			sComboCount.ChartType = SeriesChartType.Column;

			for (Int32 i = 0; i < 7; i++)
			{
				Int32 orderCount = 0;
				Int32 comboCount = 0;

				foreach (Order o in orders)
				{
					if (o.DateOrdered.DayOfWeek == (DayOfWeek)i)
					{
						orderCount += 1;
						comboCount += o.Combos.Count;
					}
				}

				sOrderCount.Points.AddXY(((DayOfWeek)i).ToString(), orderCount);
				sComboCount.Points.AddXY(((DayOfWeek)i).ToString(), comboCount);
			}

			ChartOrders.Series.Add(sOrderCount);
			ChartOrders.Series.Add(sComboCount);
		}

		private void LoadOrders_ByUser(Period period)
		{
			OrderCollection orders = LoadOrders(period);
			UserCollection users = new UserCollection(DataAccess.User_Get(SessionManager.LoggedStore));

			Series sOrderCount = new Series(Messages.Orders);
			Series sComboCount = new Series(Messages.Combos);

			sOrderCount.ChartType = SeriesChartType.Column;
			sComboCount.ChartType = SeriesChartType.Column;

			foreach (User user in users)
			{
				Int32 orderCount = 0;
				Int32 comboCount = 0;

				foreach (Order o in orders)
				{
					if (o.User.Id == user.Id)
					{
						orderCount += 1;
						comboCount += o.Combos.Count;
					}
				}

				sOrderCount.Points.AddXY(user.FullName, orderCount);
				sComboCount.Points.AddXY(user.FullName, comboCount);
			}

			ChartOrders.Series.Add(sOrderCount);
			ChartOrders.Series.Add(sComboCount);
		}

		private void LoadOrders_ByStore(Period period)
		{
			OrderCollection orders = LoadOrders(period);
			StoreCollection stores = new StoreCollection(DataAccess.Store_Get());

			Series sOrderCount = new Series(Messages.Orders);
			Series sComboCount = new Series(Messages.Combos);

			sOrderCount.ChartType = SeriesChartType.Column;
			sComboCount.ChartType = SeriesChartType.Column;

			foreach (Store store in stores)
			{
				Int32 orderCount = 0;
				Int32 comboCount = 0;

				foreach (Order o in orders)
				{
					if (o.Terminal.Store.Id == store.Id)
					{
						orderCount += 1;
						comboCount += o.Combos.Count;
					}
				}

				sOrderCount.Points.AddXY(store.Title, orderCount);
				sComboCount.Points.AddXY(store.Title, comboCount);
			}

			ChartOrders.Series.Add(sOrderCount);
			ChartOrders.Series.Add(sComboCount);
		}

		#endregion
	}
}